import config from 'config';
import httpFetch from 'share/httpFetch';
import {message} from 'antd';

const newConsumerServer = (context) => ({
  // 获取消费商介绍
  async getDescription(type) {
    try {
      context.setState({loading: true});
      const resp = await httpFetch.get(`/vendor-info-service/api/solr/vendor/introduce?vendorsId=${type}`);
      return resp;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 获取消费商
  async getCardList() {
    try {
      context.setState({loading: true});
      const resp = await httpFetch.get('/vendor-info-service/api/solr/group/index/v2');
      if (resp.status === 200) {
        context.setState({
          vendorInfo: resp.data.body,
          openVendorsList: resp.data.body.openVendorsList,
          notOpenVendorsList: resp.data.body.notOpenVendorsList || [],
        });
      }
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 获取美亚公司列表
  async getMeiyaList(params) {
    try {
      const url = `vendor=meiya&page=${params.current - 1}&size=${params.pageSize}${params.key ? `&key=${params.key}` : ''}`;
      context.setState({loading: true});
      const res = await httpFetch.get(`${config.baseUrl}/vendor-info-service/api/solr/companies/vendor?${url}`);
      if (res.status === 200) {
        const {pagination} = context.state;
        pagination.total = res.data.body.total || 0;
        context.setState({
          data: res.data.body.companyList,
          pagination,
        });
      }
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 获取携程公司列表
  async getCtripList(params) {
    try {
      const url = `vendor=ctrip&page=${params.current - 1}&size=${params.pageSize}${params.key ? `&key=${params.key}` : ''}`;
      context.setState({loading: true});
      const res = await httpFetch.get(`${config.baseUrl}/vendor-info-service/api/solr/companies/vendor?${url}`);
      if (res.status === 200) {
        const {pagination} = context.state;
        pagination.total = res.data.body.total || 0;
        context.setState({
          data: res.data.body.companyList,
          pagination,
        });
      }
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 获取滴滴公司列表
  async getDidiList(params) {
    try {
      const url = `vendor=didi&page=${params.current - 1}&size=${params.pageSize}${params.key ? `&key=${params.key}` : ''}`;
      context.setState({loading: true});
      const res = await httpFetch.get(`${config.baseUrl}/vendor-info-service/api/solr/companies/vendor?${url}`);
      if (res.status === 200) {
        const {pagination} = context.state;
        pagination.total = res.data.body.total || 0;
        context.setState({
          data: res.data.body.companyList,
          pagination,
        });
      }
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 刷新滴滴ID
  async refreshDidiId(companyOID) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get(`/api/v2/didi/company/configs?companyOID=${companyOID}`);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 提交美亚公司配置
  async submitEditData(params) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.post('/api/meiya/company/bind', params);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 提交携程公司配置
  async submitCritpData(params) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.post('/api/ctrip/company/bind', params);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 提交滴滴公司配置
  async submitDidiData(params) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.post('/api/vendors/service/company/bind', params);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 新建携程小客户账号
  async creatCritpAccount(params) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.post('/api/ctrip/company/register/v2', params);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 导出配置
  async exportSettingData(type) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get(`/vendor-info-service/api/vendors/company/config/export?vendor=${type}`);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 同步员工数据
  async syncEmployeeData(type, params) {
    try {
      context.setState({loading: true});
      setTimeout(() => {
        context.setState({loading: false});
      },
      3000);
      const res = await httpFetch.post(`/api/vendors/service/vendor/user/sync?venType=${type}`, []);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 导出同步失败的数据
  async exportSyncFailData(type, batch) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get(`/vendor-info-service/api/vendors/user/failure/export?vendor=${type}&batch=${batch}`);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 查询同步结果
  async updateAsyncFailData(type) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get(`/vendor-info-service/api/vendors/user/sync/search?vendor=${type}`);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 查看集团是否开通rankName配置
  async getRankNameConfig() {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get('/api/vendors/service/config?configName=rankName');
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 保存集团是否开通rankName配置
  async saveRankNameConfig(value) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get(`/api/vendors/service/config/open?configName=rankName&open=${value}`);
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({isEdit: false, loading: false});
    }
  },

  // 查看滴滴是否开通部门数据同步配置
  async getDepSynConfig(tenantId) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get(`/api/v2/didi/company/configs/by/tenantid?tenantId=${tenantId}`);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 保存滴滴是否开通部门数据同步配置
  async saveDepSynConfig(tenantId, value) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get(`/api/v2/didi/company/alter/configs/by/tenantid?tenantId=${tenantId}&syncDepartment=${value}`);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({isDepEdit: false, loading: false});
    }
  },

  // 同步滴滴部门数据
  async syncDepData() {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get('/api/vendors/service/vendor/deportment/sync?venType=didi');
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 查询部门同步结果
  async updateSyncDepData(type, tenantId) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get(`/vendor-info-service/api/vendors/department/sync/search?vendor=${type}&tenantId=${tenantId}`);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 下载同步失败部门信息
  async downloadFailSyncDepData(type, batch) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get(`/vendor-info-service/api/vendors/department/failure/export?vendor=${type}&batch=${batch}`);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 携程 审批单前置管控 保存
  async saveSetPreControl(params) {
    try {
      context.setState({loading: true});
      const res = await httpFetch.post('/vendor-info-service/api/vendors/company/config/ctrip/preVerifyFields', params);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 获取 携程 审批单前置管控数据
  async getPreControl(id) {
    const params = {
      tenantId: id,
    };
    try {
      context.setState({loading: true});
      const res = await httpFetch.get('/vendor-info-service/api/vendors/company/config/ctrip/preVerifyFields', params);
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

  // 查看携程appkay列表
  async getCtripAPPKeyList() {
    try {
      context.setState({loading: true});
      const res = await httpFetch.get('/vendor-info-service/api/solr/ctrip/small/key/v2?vendor=ctrip');
      return res;
    } catch (err) {
      dealWithErrorMes(err);
    } finally {
      context.setState({loading: false});
    }
  },

});

const dealWithErrorMes = (err) => {
  message.error(`error:${err.response.data.message}`);
};

export default newConsumerServer;
